# coding=utf-8
"""
Author: Mr.夏末
@Function: web自动化：https://www.yuque.com/docs/share/73a7c983-4fcd-4bcf-8838-de0675b1738a
"""
import re
import time

from selenium.webdriver import ActionChains

from homework import Web

from selenium import webdriver

# 创建浏览器对象
# driver = webdriver.Chrome()
web = Web()
web.oprn_browser('gc')
driver = web.driver
# 添加隐式等待
driver.implicitly_wait(5)

# 打开网站
driver.get("http://47.105.110.138:8000/index.php/Home/user/login.html")
# 最大化浏览器
driver.maximize_window()

#ID定位
ele = driver.find_element('id','username')
# 操作元素:13800138006/2529085125@qq.com
ele.send_keys('13800138006')

ele = driver.find_element('link text','淘宝网')
print(ele.get_attribute('outerHTML'))
# 部分文本
ele = driver.find_element("partial link text",'淘宝')
print(ele.get_attribute('outerHTML'))

# 输入密码
driver.find_element('xpath','//*[@id="password"]').send_keys('123456')
# 输入验证码
driver.find_element('xpath','//*[@id="verify_code"]').send_keys('1234')
# 点击登录
# driver.find_element('xpath','//*[@id="loginform"]/div/div[6]/a').click
ele = driver.find_element('partial link text','登')
# js获取返回值
s = driver.execute_script('return arguments[0].style',ele)
print(s)
# js点击登录
driver.execute_script('arguments[0].click()',ele)
# driver.execute_script('arguments[1].click();alert("arguments[0]")',111,ele)

# 获取昵称
nickname = driver.find_element('xpath','//a[@class="red userinfo"]').text
print(nickname)

#搜索
driver.find_element('xpath','//*[@id="q"]').send_keys('手机')
driver.find_element('xpath','//*[@id="sourch_form"]/a').click()

# 点击第1个加入购物车
driver.find_element('link text','加入购物车').click()
driver.find_element('id','join_cart').click()
# 强制等待
time.sleep(1)
driver.find_element('xpath','//*[@id="layui-layer1"]/span/a').click()

# 结算
driver.find_element('xpath','//*[@id="hd-my-cart"]/a/div/span').click()
driver.find_element('link text','去结算').click()
driver.find_element('class name','checkout-submit').click()

# 获取订单号
print(driver.find_element('xpath','//p[@class="succ-p"]').text)
orders = driver.find_element('xpath','//p[@class="succ-p"]').text
result = re.findall(r'\d{18}',orders)
print(result)
time.sleep(1)
driver.find_element('link text','我的订单').click()
# 切换窗口
driver.switch_to.window(driver.window_handles[1])
# 搜索订单号
driver.find_element('xpath','//*[@id="search_key"]').send_keys(result)
driver.find_element('xpath','//*[@id="search_order"]/input[2]').click()

# 悬停
ele = driver.find_element('xpath','//span[text()="账户设置"]')
# 创建对象
action = ActionChains(driver)
# 鼠标移动到元素上
action.move_to_element(ele).perform()

# 修改个人信息
driver.find_element('xpath',"//a[text()='个人信息']").click()
# driver.find_element('link text','个人信息').click()
driver.find_element('xpath','//*[@id="preview"]').click()
driver.switch_to.frame(driver.find_element('xpath','//iframe'))
driver.find_element('xpath','//div[@id="filePicker"]/div[2]/input').send_keys(r'C:\Users\17863\Pictures\nn.jpg')
time.sleep(1)
driver.find_element('xpath','//div[@class="saveBtn"]').click()
driver.switch_to.default_content()
driver.find_element('xpath','//input[@class="save"]').click()

# 地址管理
driver.find_element('link text','地址管理').click()
